/* 
** (Copyright) sejishikong <sejishikong@263.net>
** Authors:    sejishikong <sejishikong@263.net>
**             Wei Lian <lianwei2003.student@sina.com> 
** Maintainer: Wei Lian
**    This program is free software; you can redistribute it and/or modify
**    it under the terms of the GNU General Public License Version 2 as
**    published by the Free Software Foundation;
**
**    This program is distributed in the hope that it will be useful,
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**    GNU General Public License for more details.
**
**    You should have received a copy of the GNU General Public License
**    along with this program; if not, write to the Free Software
**    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
*/

#include "header.h"
#include "mainwindow.h"
#include "mylistitem.h"
#include "config.h"
#include "fork.h"
#include "channel.h"
#include "menubar.h"
#include "sys.h"
////slots

void
Sopcast::timerDone ()
{
  ///sound
  int value = sys->sound_read ();
  if (slidersound->value () != value)
    slidersound->setValue (value);
  ///player
  if (fork->snstatus && buttonplayer->isOn () && !fork->player->isRunning ())
    {
      if (fork->forkplayer () == TRUE)
	buttonplayer->setPaletteForegroundColor (Qt::blue);
      else
	fprintf (stderr, "failed to launch player!\n");
    }

}


///-------------------------------------------------------
void
Sopcast::onlistitemclicked (QListViewItem * item)
{
  static int id = 1;
  if (item->depth () == 1)
    {
      fork->killsop ();
      fork->killplayer ();
      channelname = item->text (NAME_COLUMN);
      if (fork->forksop (item->text (URL_COLUMN)) == TRUE)
	{
	  message[0]->setText ("Connecting...");
	  ///add to history
	  int index = 0;
	  bool flag = FALSE;
	  while (menubar->history->idAt (index) != -1)
	    {
//        printf ("index=%d,id=%d\n", index, menubar->history->idAt (index));
	      if (menubar->history->text (menubar->history->idAt (index)) ==
		  (item->text (NAME_COLUMN) + " " + item->text (URL_COLUMN)))
		flag = TRUE;
	      index++;
	    }

	  if (flag == FALSE)
	    {
	      menubar->history->insertItem (item->text (NAME_COLUMN) + " " +
					    item->text (URL_COLUMN), id);
	      id++;
	    }
	}
      else
	fprintf (stderr, "failed to launch sp-sc!\n");
    }
}

///-------------------------------------------------------
void
Sopcast::onButtonPlayerToggled (bool t)
{
  if (t)
    {
      if (fork->snstatus)
	{
	  fork->killplayer ();
	  if (fork->forkplayer () == TRUE)
	    buttonplayer->setPaletteForegroundColor (Qt::blue);
	  else
	    fprintf (stderr, "failed to launch player!\n");
	}
    }
  else
    fork->killplayer ();
}

void
Sopcast::onbuttonstopclicked ()
{
  fork->killsop ();
  fork->killplayer ();
}

void
Sopcast::onbuttonquitclicked ()
{
  fork->killsop ();
  fork->killplayer ();
}

void
Sopcast::onbuttonexpandclicked ()
{
  bool flag;
  if (buttonexpand->paletteForegroundColor () == Qt::black)
    {
      flag = TRUE;
      buttonexpand->setPaletteForegroundColor (Qt::blue);
    }
  else
    {
      flag = FALSE;
      buttonexpand->setPaletteForegroundColor (Qt::black);
    }

  QListViewItem *myChild = channel->firstChild ();
  while (myChild)
    {
      if (flag)
	{
	  myChild->setOpen (TRUE);
	}
      else
	{
	  myChild->setOpen (FALSE);
	}

      myChild = myChild->nextSibling ();
    }
}


void
Sopcast::onbuttonsearchclicked ()
{
  if (channel->currentItem () == 0 || editsearch->text ().isEmpty ())
    return;

  bool flagfirst = TRUE;
  QListViewItem *item, *childitem, *focusitem = 0;
  ////assign initial value
  if (channel->currentItem ()->depth () == 0)
    {
      item = channel->currentItem ();
      childitem = item->firstChild ();
    }
  else
    {
      childitem = channel->currentItem ();
      item = childitem->parent ();
      childitem = childitem->nextSibling ();
    }
  /////////find focus
  while (1)
    {
      while (childitem)
	{
	  if (childitem->text (NAME_COLUMN).
	      contains (editsearch->text (), false)
	      && childitem->isEnabled ())
	    {
	      channel->setCurrentItem (childitem);
	      focusitem = childitem;
	      flagfirst = FALSE;
	      goto EXIT;
	    }
	  childitem = childitem->nextSibling ();
	}
      item = item->nextSibling ();
      if (!item)
	break;
      childitem = item->firstChild ();
    }
EXIT:;
  ////set color and find first focus if having failed finding focus so far
  item = channel->firstChild ();
  while (item)
    {
      bool flag = FALSE;
      childitem = item->firstChild ();
      while (childitem)
	{
	  if (childitem->text (NAME_COLUMN).
	      contains (editsearch->text (), false)
	      && childitem->isEnabled ())
	    {
	      static_cast < MyListItem * >(childitem)->bcolor = Qt::yellow;
	      childitem->repaint ();

	      flag = TRUE;
	      if (flagfirst == TRUE)
		{
		  channel->setCurrentItem (childitem);
		  focusitem = childitem;
		  flagfirst = FALSE;
		}
	    }
	  else
	    {
	      static_cast < MyListItem * >(childitem)->bcolor = Qt::white;
	      childitem->repaint ();
	    }
	  childitem = childitem->nextSibling ();
	}

      if (flag)
	{
	  item->setOpen (TRUE);
	}
      else
	{
	  item->setOpen (FALSE);
	}

      item = item->nextSibling ();
    }

//////////////////scroll to the first found item
  if (focusitem == 0)
    return;

  int value = channel->itemPos (focusitem) - channel->height () / 2;
  channel->verticalScrollBar ()->setValue (value);
}


///---------------------------------------------
void
Sopcast::updateopenstate ()
{
  QListViewItem *myChild = channel->firstChild ();
  int count = 0;
  while (myChild)
    {
      if (myChild->isOpen ())
	stateopen[count++] = 1;
      else
	stateopen[count++] = 0;

      myChild = myChild->nextSibling ();
    }
}
